<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Shop;

class ShopController extends Controller
{
    public function index()
    {
        $lists = Shop::where('status', '!=', -1)->paginate(15);
        return view('admin.store.index', compact('lists'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.store.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $datas = $request->except(['_token']);
        $res = Shop::create($datas);
        if ($res) {
            return parent::success('success', route('admin.shops.index'));
        } else {
            return parent::error('error');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id, Request $request)
    {
        $shop = Shop::find($id);
        return view('admin.store.edit', compact('shop'));        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id = $request->id;
        $datas = $request->except('_token', 'id', '_method');
        $update = Shop::where('id', $id)->update($datas);

        if ($update) {
            return parent::success('success', route('admin.shops.index'));
        } else {
            return parent::error('error');
        }
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $update = Shop::where('id', $id)->update(['status'=>-1]);
        if ($update) {
            return parent::success('success');
        } else {
            return parent::error('error');
        }  
    }

    public function destroyAll(Request $request)
    {
        $ids = $request->shop_id;
        // return parent::success($ids);
        $update = Shop::whereIn('id', $ids)->update(['status'=>-1]);

        if ($update) {
            return parent::success('success', route('admin.shops.index'));
        } else {
            return parent::error('error');
        } 
    }

    public function publish($id)
    {
        $update = Shop::where('id', $id)->update(['status'=>1]);
        if ($update) {
            return parent::success('success');
        } else {
            return parent::error('error');
        }        
    }

    public function unpublish($id)
    {
        $update = Shop::where('id', $id)->update(['status'=>0]);
        if ($update) {
            return parent::success('success');
        } else {
            return parent::error('error');
        }        
    }

}
